package com.tanhua.server.service;

import com.tan.model.domain.ErrorResult;
import com.tan.model.domain.UserInfo;
import com.tan.model.enums.CommentType;
import com.tan.model.mongo.Comment;
import com.tan.model.vo.CommentVo;
import com.tan.model.vo.LikeVo;
import com.tan.model.vo.PageResult;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.MoventApi;
import com.tanhua.dubbo.api.UserInfoapi;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.Userthread;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class CommentService {
    @DubboReference
    private CommentApi commentApi;
    @DubboReference
    private MoventApi moventApi;
    @DubboReference
    private UserInfoapi userInfoApi;
    public void publishComment(String movementId, String comment) {
        Long UserId = Userthread.getUser().getId();
      Integer integer =  commentApi.publishComment(movementId,comment,UserId, CommentType.COMMENT.getType());
        log.info( integer.toString());
    }

    public PageResult getComments(String movementId, Integer page, Integer pagesize) {
        //PageReslt--comment
    PageResult pageResult = commentApi.getComments(movementId, page, pagesize);
    if(pageResult.getItems()==null){
        return pageResult;
    }
    List<Comment> items = (List<Comment>) pageResult.getItems();
    List<CommentVo> list = new ArrayList<>();
    for (Comment item : items) {

              UserInfo userInfo = userInfoApi.getUserInfo(item.getUserId());
              CommentVo commentVo = CommentVo.init(userInfo, item);
              list.add(commentVo);
    }
    PageResult pr = new PageResult(page,pagesize,list.size(),list);
    return pr;

    }

    public Integer getLikecommentCount(String commentId) {
     Integer integer =   commentApi.savecommentlike(commentId);
        return integer;


    }

    public Integer getdisLikecommentCount(String commentid) {
        Integer integer =  commentApi.deletecommentlike(commentid);
        return integer;
    }



}
